<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" class="tl-dialog" width="1200px" @open="open" @close="close" top="50px" :close-on-click-modal="false" :close-on-press-escape="false">
    <!-- 查询 -->
    <tl-list-search ref="search" style="margin:6px 0" :query-param="queryParam" @search="doQuery" :isDisplaySearch="false" :isDisplayDefaultSearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <el-form-item class="qryItem" label="统计周期">
          <el-date-picker :picker-options="rangeOption"  v-model="startandenddate" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" @change="changedate"></el-date-picker>
        </el-form-item>
        <el-form-item class="qryItem" label="项目级别">
          <el-select v-model="queryParam.projectlevel"  multiple :collapse-tags="true" clearable>
            <el-option v-for="item in codeModel.projectlevelList" :key="item.code" :label="item.name" v-if="item.code != '3'" :value="item.code" clearable />
          </el-select>
        </el-form-item>
        <el-form-item class="qryItem" label="部门">
          <el-select v-model="queryParam.deptcommoncode"  multiple :collapse-tags="true" filterable clearable>
            <el-option v-for="item in codeModel.deptCommonCodeList" :key="item.code" :label="item.name" :value="item.code" clearable />
          </el-select>
        </el-form-item>
      </template>
      <template slot="search">
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="loadData()">搜索</el-button>
      </template>
    </tl-list-search>
    <tl-list-toolbar ref="toolbar" @add="openAddDialog('editForm')" @exportExcel="doExportExcelWithHeader" @deleteBatch="doDeleteBatch" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :disableDeleteBatch="selectedRowKeys.length <= 0" :isDisplayAdd="false" :isDisplayDeleteBatch="false">
      <template slot="left-btn">
        <el-switch v-model="queryParam.viewProjectManager" @change="loadData()" active-text="项目经理"></el-switch>
      </template>
    </tl-list-toolbar>
    <el-table ref="mainTable" class="tl-table" :data="datas" border fit :resizable-refs="tableResizeRefs" v-loading="loading">
      <el-table-column label="序号" prop="index" width="60px" type="index"></el-table-column>
      <el-table-column prop="dispdeptcommoncode" label="部门" min-width="110px" align="center" header-align="center"></el-table-column>
      <el-table-column v-if="queryParam.viewProjectManager" prop="dispprojectmanagercode" label="项目经理" width="100px" align="center" header-align="center"></el-table-column>
      <el-table-column prop="count" label="招标项目数量" width="120px" align="center" header-align="center"></el-table-column>
      <el-table-column v-for="pre in prefix" :key="randomid(pre.code)" :label="pre.name" width="300px" align="center" header-align="center">
        <el-table-column v-for="col in columnName" align="center" :key="pre.code + '_' + col.code" :label="col.name" :prop="pre.code + '_' + col.code"></el-table-column>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script>
import { getAction, postAction } from '@tapui/utils/http'
import { validateNull } from '@tapui/utils/validate'
import { randomUUID } from '@tapui/utils/util'
import { formatDate } from '@tapui/utils/date'
import { mixin_list } from '@tapui/mixins/mixin_list'
import { mixin_multiheader_excel } from '@/mixins/mixin_multiheader_excel'
export default {
  name: 'BidScoreThreePhase',
  mixins: [mixin_list, mixin_multiheader_excel],
  data() {
    return {
      title: '评分细则三阶段自评统计报表',
      visible: false,
      datas: [],
      url: {
        list: '/mpm/projectBidScore/threePhaseBidScore',
        queryCodeList: '/mpm/projectBidScore/dataCodeList',
        exportExcelMergeHeader: '/mpm/projectBidScore/exportNewExcel'
      },
      queryParam: {
        deptcommoncode: [],
        projectlevel: ['0'],
        viewProjectManager: false,
        startDate: formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), 1), 'yyyy-MM-dd'),
        endDate: formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), 31), 'yyyy-MM-dd')
      },
      get rangeOption() {
        // 解决默认写法 this 指向问题
        let that = this
        return {
          disabledDate(date) {
            let now = new Date()
            let tempdate = new Date(date)
            // 往年不控制 未来年份禁用
            if (now.getFullYear() > tempdate.getFullYear()) {
              return false
            } else {
              return now.getMonth() < tempdate.getMonth()
            }
          }
        }
      },
      startandenddate: [],
      reduceHeight: 150,
      prefix: [{ code: 'first', name: '一阶段自评（立项阶段）' }, { code: 'second', name: '二阶段自评（挂网阶段）' }, { code: 'third', name: '三阶段自评（复盘阶段）' }],
      columnName: [{ code: 'expectsum', name: '应录数量' }, { code: 'actualsum', name: '已录数量' }, { code: 'rate', name: '录入率' }]
    }
  },
  computed: {
    randomid() {
      return function(key) {
        return key + randomUUID()
      }
    }
  },
  methods: {
    open() {
      this.startandenddate.push(this.getNowMonthFirst())
      this.startandenddate.push(this.getNowMonthLast())
      this.changedate(this.startandenddate)
      this.loadData()
    },
    close() {
      this.datas = []
      this.startandenddate = []
      this.projectlevel = ['0']
    },
    getNowMonthFirst() {
      const date = new Date()
      date.setDate(1)
      return formatDate(date, 'yyyy-MM-dd')
    },
    getNowMonthLast() {
      const date = new Date()
      const enddate = new Date(date.getFullYear(), date.getMonth() + 1, 0)
      return formatDate(enddate, 'yyyy-MM-dd')
    },
    changedate(data) {
      if (validateNull(data)) {
        this.queryParam.startDate = null
        this.queryParam.endDate = null
      } else {
        this.queryParam.startDate = data[0]
        this.queryParam.endDate = data[1]
      }
    },
    loadData(paramMap) {
      if (!this.beforeLoadData()) return
      // 查询条件
      var params = this.getQueryParams()
      // 数据查询
      this.loading = true
      postAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            this.datas = res.result
            this.pageInfo.totalCount = res.result.totalCount
            // 初始化自定义列
            if (typeof this.$refs.toolbar !== 'undefined') {
              this.$refs.toolbar.doInitTableColumn()
            }
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterLoadData()
          this.loading = false
        })
        .catch(err => {
          this.loading = false
        })
    }
  }
}
</script>

<style lang="scss" scoped></style>
